package com.bzxhh.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

//演示读写锁降级
public class Demo02 {

    public static void main(String[] args) {
        //可重入对写锁对象
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

//        //降级
//        //1.获取写锁
//        writeLock.lock();
//        System.out.println("bzxhh");
//
//        //2.获取读锁
//        readLock.lock();
//        System.out.println("read");
//
//        //3.释放写锁
//        writeLock.unlock();
//
//        //4.释放读锁
//        readLock.unlock();

        //升级
        //2.获取读锁
        readLock.lock();
        System.out.println("read");

        //1.获取写锁
        writeLock.lock();
        System.out.println("bzxhh");
        
        //3.释放写锁
        writeLock.unlock();

        //4.释放读锁
        readLock.unlock();
    }
}
